﻿--[[	$Id: caelCCBreak.lua 2574 2012-09-21 12:24:11Z sdkyron@gmail.com $	]]

local _, caelCCBreak = ...

caelCCBreak.eventFrame = CreateFrame("Frame", nil, UIParent)

local grouped = nil
--local msg = "|cffD7BEA5%s|r on |cffAF5050%s|r removed by |cff559655%s|r%s"
local msg = caelLib.locale == "frFR" and "%s %s enlevé par |cff559655%s|r" or "%s %s removed by |cff559655%s|r"

local hostile = COMBATLOG_OBJECT_REACTION_HOSTILE or 64 or 0x00000040

local GetSpellName = function(spellId)
	return GetSpellInfo(spellId)
end

-- Note: We are only considering crowd controls lasting at least 20 seconds

local spells = {
	GetSpellName(118), -- Polymorph
	GetSpellName(28272), -- Polymorph (pig)
	GetSpellName(28271), -- Polymorph (turtle)
	GetSpellName(59634), -- Polymorph (penguin)
	GetSpellName(61025), -- Polymorph (Serpent)
	GetSpellName(61305), -- Polymorph (black cat)
	GetSpellName(61721), -- Polymorph (rabbit)
	GetSpellName(61780), -- Polymorph (turkey)

	GetSpellName(9484), -- Shackle Undead

	GetSpellName(3355), -- Freezing Trap
	GetSpellName(19386), -- Wyvern Sting

	GetSpellName(339), -- Entangling Roots
	GetSpellName(2637), -- Hibernate

	GetSpellName(6770), -- Sap

	GetSpellName(5782), -- Fear
	GetSpellName(6358), -- Seduction (succubus)

	GetSpellName(10326), -- Turn Evil
	GetSpellName(20066), -- Repentance

	GetSpellName(51514), -- Hex
	GetSpellName(76780), -- Bind Elemental
}

caelCCBreak.eventFrame:RegisterEvent("GROUP_ROSTER_UPDATE")
caelCCBreak.eventFrame:SetScript("OnEvent", function(self, event, _, subEvent, _, _, sourceName, _, _, _, destName, destFlags, _, spellId, spellName, _, _, extraSpellName, ...)
	if event == "GROUP_ROSTER_UPDATE" then
		local n = GetNumGroupMembers()
		if not grouped and n > 0 then
			self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
			grouped = true
		elseif grouped and n == 0 then
			self:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
			grouped = nil
		end
	else
		if subEvent == "SPELL_AURA_BROKEN_SPELL" or subEvent == "SPELL_AURA_BROKEN" then
			if bit.band(destFlags, hostile) == hostile then
				for k, v in pairs(spells) do
					if v == spellName then
--						DEFAULT_CHAT_FRAME:AddMessage(msg:format(spellName, destName, sourceName and sourceName or "Unknown", extraSpellName and "'s "..extraSpellName or ""))
						DEFAULT_CHAT_FRAME:AddMessage(msg:format("|cffD7BEA5cael|rCCBreak:", GetSpellLink(spellId), sourceName and sourceName or "Unknown"))
						break
					end
				end
			end
		end
	end
end)